home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Tool Chest / Dev.CD Feb 97 TC.toast / Sample Code / QuickTime / JPEG Sample / Source / events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-22  |  12.1 KB  |  586 lines  |  [TEXT/MPCC]

  1. /*************************************************************************************
  2. #
  3. #        events.c
  4. #
  5. #        This segment handles the basic event calls.
  6. #
  7. #        Author(s):     Michael Marinkovich & Guillermo Ortiz
  8. #                    Apple Developer Technical Support
  9. #                    marink@apple.com
  10. #
  11. #        Modification History: 
  12. #
  13. #            4/3/96        MWM     Initial coding                     
  14. #
  15. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  16. #
  17. #
  18. #        You may incorporate this sample code into your applications without
  19. #        restriction, though the sample code has been provided "AS IS" and the
  20. #        responsibility for its operation is 100% yours.  However, what you are
  21. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  22. #        after having made changes. If you're going to re-distribute the source,
  23. #        we require that you make it clear in the source that the code was
  24. #        descended from Apple Sample Code, but that you've made changes.
  25. #
  26. *************************************************************************************/
  27.  
  28. #include <Events.h>
  29. #include <ToolUtils.h>
  30. #include <Gestalt.h>
  31. #include <OSUtils.h>
  32. #include <Palettes.h>
  33.  
  34. #include "App.h"
  35. #include "Proto.h"
  36.  
  37. extern Boolean        gInBackground;
  38. extern Boolean        gDone;
  39. extern Boolean        gHasAbout;        // have an about box?
  40.  
  41.  
  42. //----------------------------------------------------------------------
  43. //
  44. //    EventLoop - main entry and loop for all event processing
  45. //
  46. //
  47. //----------------------------------------------------------------------
  48.  
  49. void EventLoop(void)
  50. {
  51.     EventRecord        event;
  52.     RgnHandle        cursorRgn;
  53.     Boolean            gotEvent;
  54.         
  55.     gDone = false;
  56.     cursorRgn = NewRgn();
  57.         
  58.     do {
  59.         gotEvent = WaitNextEvent(everyEvent,&event,MyGetSleep(),cursorRgn);
  60.                                     
  61.         if (gotEvent)
  62.             DoEvent(&event);
  63.  
  64.                         
  65.     } while ( !gDone );
  66.         
  67.     DisposeRgn(cursorRgn);
  68. }
  69.  
  70.  
  71. //----------------------------------------------------------------------
  72. //
  73. //    MyGetSleep - return sleep value based upon whether or not the app
  74. //                 is in the background.
  75. //
  76. //----------------------------------------------------------------------
  77.  
  78. short MyGetSleep(void)
  79. {
  80.     short        sleep = 30;
  81.     
  82.     if (gInBackground)
  83.         sleep = 1L;
  84.  
  85.     return sleep;
  86.  
  87. }
  88.  
  89.  
  90. //----------------------------------------------------------------------
  91. //
  92. //    CustomWindowEvent - Handles custom procs assigned to a window. 
  93. //                        Different window kinds can easily have unique event
  94. //                        handlers, ie. floaters, dialogs, documentprocs
  95. //----------------------------------------------------------------------
  96.  
  97. void CustomWindowEvent(short eventType,WindowRef window,void *refCon)
  98. {
  99.     CustomProc        theProc;
  100.     short            kind;
  101.     DocHnd            doc;
  102.     
  103.     kind = GetWindKind(window);
  104.     if (kind < kDocKind || kind > kAboutKind)     // not our window
  105.         return;
  106.  
  107.     doc = (DocHnd)GetWRefCon(window);
  108.     
  109.     if (doc != nil) {
  110.         HLockHi((Handle)doc);
  111.         switch(eventType) {
  112.             case kIdleProc:
  113.                 theProc = (**doc).idleProc;
  114.                 break;
  115.                 
  116.             case kMenuProc:
  117.                 theProc = (**doc).mMenuProc;
  118.                 break;
  119.  
  120.             case kInContentProc:
  121.                 theProc = (**doc).inContentProc;
  122.                 break;
  123.  
  124.             case kInGoAwayProc:
  125.                 theProc = (**doc).inGoAwayProc;
  126.                 break;
  127.                 
  128.             case kInZoomProc:
  129.                 theProc = (**doc).inZoomProc;
  130.                 break;
  131.                 
  132.             case kInGrowProc:
  133.                 theProc = (**doc).inGrowProc;
  134.                 break;
  135.                 
  136.             case kMUpProc:
  137.                 theProc = (**doc).mUpProc;
  138.                 break;
  139.                 
  140.             case kKeyProc:
  141.                 theProc = (**doc).keyProc;
  142.                 break;
  143.             
  144.             case kActivateProc:
  145.                 theProc = (**doc).activateProc;
  146.                 break;
  147.  
  148.             case kUpdateProc:
  149.                 theProc = (**doc).updateProc;
  150.                 break;
  151.  
  152.             default:
  153.                 theProc = nil;
  154.                 break;
  155.         }        
  156.     
  157.         if (theProc != nil) {            
  158.             (*theProc)(window,refCon);
  159.             HUnlock((Handle)doc);
  160.     
  161.         }
  162.     }    
  163.     
  164.     
  165. }
  166.  
  167.  
  168. //----------------------------------------------------------------------
  169. //
  170. //    DoEvent - event dispatcher, called by eventloop
  171. //                
  172. //
  173. //----------------------------------------------------------------------
  174.  
  175. void DoEvent(EventRecord *event)
  176. {
  177.     OSErr            err;
  178.     short            kind;
  179.     long            menuChoice;
  180.     Point            thePoint;
  181.     Boolean            active;
  182.     WindowRef        window;
  183.     
  184.     window = FrontWindow();
  185.  
  186.     switch(event->what) {
  187.         case nullEvent:
  188.             CustomWindowEvent(kIdleProc, window, nil);
  189.             break;
  190.             
  191.         case mouseDown:
  192.             HandleMouseDown(event);
  193.             break;
  194.                             
  195.         case mouseUp:
  196.             break;
  197.                             
  198.         case keyDown:
  199.         case autoKey:
  200.             if (event->modifiers & cmdKey) {                 //    is cmd key down
  201.                 menuChoice = MenuKey(event->message & charCodeMask);
  202.                 kind = GetWindKind(window);
  203.                 if (kind < kDocKind || kind > kFloatKind)             // not our window
  204.                     HandleMenuChoice(window, (void *)&menuChoice);    // default menu
  205.                 else    
  206.                     CustomWindowEvent(kMenuProc, window, (void *)&menuChoice);
  207.             }
  208.             break;
  209.                             
  210.         case activateEvt:
  211.             gInBackground = event->modifiers & activeFlag;
  212.             active = gInBackground;
  213.             CustomWindowEvent(kActivateProc, (WindowRef)event->message, &active);
  214.             break;
  215.                             
  216.         case updateEvt:
  217.             UpdateWindow((WindowRef)event->message);
  218.             break;
  219.                             
  220.         case diskEvt:
  221.             if (HiWord(event->message) != noErr) {
  222.                 SetPt(&thePoint, 50, 50);
  223.                 err = DIBadMount(thePoint, event->message);
  224.             }
  225.             break;
  226.                             
  227.         case osEvt:
  228.             switch ((event->message >> 24) & 0x0FF) {        
  229.                 case suspendResumeMessage:    
  230.                     gInBackground = event->message & resumeFlag;
  231.                     active = gInBackground;
  232.                     CustomWindowEvent(kActivateProc, FrontWindow(), &active);
  233.                     break;
  234.             }
  235.             break;
  236.     
  237.         case kHighLevelEvent:
  238.             AEProcessAppleEvent(event);
  239.             break;
  240.     }
  241.  
  242. }            
  243.  
  244.  
  245.  
  246. //----------------------------------------------------------------------
  247. //
  248. //    DoIdle - handle Idle events
  249. //                
  250. //
  251. //----------------------------------------------------------------------
  252.  
  253. void DoIdle(WindowRef window, void *refCon)
  254. {
  255.  
  256. }
  257.  
  258.  
  259. //----------------------------------------------------------------------
  260. //
  261. //    HandleMouseDown - 
  262. //                
  263. //
  264. //----------------------------------------------------------------------
  265.  
  266. void HandleMouseDown(EventRecord *event)
  267. {
  268.     long            menuChoice;
  269.     short            thePart;
  270.     short            kind;
  271.     WindowRef        window;
  272.         
  273.  
  274.     thePart = FindWindow(event->where,&window);
  275.         
  276.     switch(thePart) {
  277.         case inMenuBar:
  278.             menuChoice = MenuSelect(event->where);
  279.             window = FrontWindow();
  280.             kind = GetWindKind(window);
  281.             if (kind < kDocKind || kind > kAboutKind)             // not our window
  282.                 HandleMenuChoice(window, (void *)&menuChoice);    // default menu
  283.             else    
  284.                 CustomWindowEvent(kMenuProc, window, (void *)&menuChoice);
  285.             break;
  286.  
  287.         case inContent:
  288.             if (window != FrontWindow())
  289.                 SelectWindow(window);
  290.             else
  291.                 CustomWindowEvent(kInContentProc, window, &event->where);
  292.     
  293.             break;
  294.  
  295.         case inSysWindow:
  296.             SystemClick(event,window);
  297.             break;
  298.                                                 
  299.         case inDrag:
  300.             if (window != FrontWindow())
  301.                 SelectWindow(window);
  302.             DragWindow(window, event->where,&qd.screenBits.bounds);
  303.             break;
  304.                         
  305.         case inGoAway:
  306.             if (TrackGoAway(window, event->where))
  307.                 RemoveWindow(window);
  308.             break;
  309.                         
  310.         case inZoomIn:
  311.         case inZoomOut:
  312.             if (TrackBox(window,event->where,thePart)) 
  313.                 CustomWindowEvent(kInZoomProc, window,&thePart);
  314.             break;
  315.                         
  316.         case inGrow:
  317.             CustomWindowEvent(kInGrowProc, window, &event->where);
  318.             break;
  319.     }
  320.     
  321. }
  322.  
  323.  
  324. //----------------------------------------------------------------------
  325. //
  326. //    HandleMenuChoice - 
  327. //                
  328. //
  329. //----------------------------------------------------------------------
  330.  
  331. void HandleMenuChoice(WindowRef window, void *refCon)
  332. {
  333.     OSErr        err = noErr;
  334.     long         menuChoice;
  335.     short        item, menu;
  336.     short        daRefNum;
  337.     Rect        bounds;
  338.     Str255        daName;
  339.     WindowRef    newWindow;
  340.     
  341.     
  342.     menuChoice = *(long *)refCon;
  343.     
  344.     item = LoWord(menuChoice);
  345.     menu = HiWord(menuChoice);
  346.  
  347.     switch(menu) {
  348.         case mApple:
  349.             switch(item) {
  350.                 case iAbout:
  351.                     if ( !gHasAbout ) {
  352.                         SetRect( &bounds,2, 40 ,352 ,140 );
  353.                         newWindow = CreateWindow(nil, nil, &bounds, "\pAbout", true,
  354.                                                  documentProc, kAboutKind, (WindowPtr)-1,
  355.                                                  true, nil );
  356.                         gHasAbout = true;
  357.                     }                        
  358.                     break;
  359.                     
  360.                 default:
  361.                     GetItem(GetMHandle(mApple),item,daName);
  362.                     daRefNum = OpenDeskAcc( daName );
  363.                     break;
  364.             }    
  365.             break;
  366.                     
  367.         case mFile:
  368.             switch(item) {
  369.                 case iNew:
  370.                     SetRect(&bounds,100,100,300,300);
  371.                     newWindow = CreateWindow(128, nil, &bounds, nil, false,
  372.                                             documentProc, kDocKind, (WindowPtr)-1,
  373.                                             true, nil );
  374.                     break;
  375.                 case iOpen:
  376.                     DoOpenNew();
  377.                     break;
  378.                         
  379.                 case iSave:
  380.                     err = DoSaveJPEG(window);
  381.                     break;
  382.  
  383.                 case iClose:
  384.                     RemoveWindow(FrontWindow());
  385.                     break;    
  386.                         
  387.                 case iQuit:
  388.                     gDone = true;
  389.                     break;
  390.                     
  391.                 default:
  392.                     break;    
  393.             }
  394.             break;
  395.                     
  396.         default:
  397.             break;    
  398.         
  399.     }
  400.     
  401.     HiliteMenu(0);
  402.     
  403. }
  404.  
  405.  
  406. //----------------------------------------------------------------------
  407. //
  408. //    HandleContentClick - 
  409. //                
  410. //
  411. //----------------------------------------------------------------------
  412.  
  413. void HandleContentClick(WindowRef window, void *refCon)
  414. {
  415.     ControlRef            control;
  416.     ControlActionUPP    scrollUPP;
  417.     Point                mouse;
  418.     short                value;
  419.     short                thePart;
  420.     short                oldSetting;
  421.     short                horzScroll,vertScroll;
  422.     DocHnd                doc;
  423.  
  424.     doc = (DocHnd)GetWRefCon(window);
  425.     if (doc != nil) {
  426.         SetPort(window);
  427.         mouse = *(Point *)refCon;
  428.         GlobalToLocal(&mouse);
  429.         horzScroll = vertScroll = 0;
  430.  
  431.         if ((thePart = FindControl(mouse, window, &control)) != 0) {
  432.             switch(thePart) {
  433.                 case inThumb:
  434.                     oldSetting = GetCtlValue(control);
  435.                     thePart = TrackControl(control, mouse, 0L);
  436.                     if (thePart != 0) {
  437.                         value = oldSetting - GetCtlValue(control);
  438.                         if (value != 0){
  439.                             if (control == (**doc).hScroll)
  440.                                 horzScroll = value ;
  441.                             if (control == (**doc).vScroll)
  442.                                 vertScroll = value;
  443.                             MyScrollPicture(window, horzScroll, vertScroll);
  444.                         }
  445.                     }
  446.                     break;
  447.                 case inUpButton:
  448.                 case inDownButton:
  449.                 case inPageUp:
  450.                 case inPageDown:
  451.                     scrollUPP = NewControlActionProc(ScrollActionProc);
  452.                     value = TrackControl(control, mouse, scrollUPP);
  453.                     break;
  454.     
  455.             }
  456.         }
  457.     }
  458.  
  459. }
  460.  
  461.  
  462. //----------------------------------------------------------------------
  463. //
  464. //    HandleZoomClick - 
  465. //                
  466. //
  467. //----------------------------------------------------------------------
  468.  
  469. void HandleZoomClick(WindowRef window, void *refCon)
  470. {
  471.     short            part;
  472.     DocHnd            doc;
  473.     
  474.     doc = (DocHnd)GetWRefCon(window);
  475.     if (doc != nil) {
  476.         part = *(short *)refCon;
  477.         SetPort(window);
  478.         
  479.         EraseRect(&window->portRect);
  480.         ZoomWindow(window,part,true);
  481.         ClipRect(&window->portRect);
  482.         InvalRect(&window->portRect);
  483.         
  484.         HideControl((**doc).hScroll);
  485.         HideControl((**doc).vScroll);
  486.         
  487.         AdjustScrollbars(window, true);
  488.         
  489.         ShowControl((**doc).hScroll);
  490.         ShowControl((**doc).vScroll);
  491.         
  492.     }
  493. }
  494.  
  495.  
  496. //----------------------------------------------------------------------
  497. //
  498. //    HandleGrow - 
  499. //                
  500. //
  501. //----------------------------------------------------------------------
  502.  
  503. void HandleGrow(WindowRef window, void *refCon)
  504. {
  505.     Rect            gIRect;
  506.     Rect            limitRect;
  507.     long            growSize;
  508.         
  509.     limitRect = (**GetGrayRgn()).rgnBBox;
  510.     limitRect.left += 125;
  511.     limitRect.top += 125;
  512.  
  513.     growSize = GrowWindow(window, *(Point *)refCon, &limitRect);
  514.     if (growSize) {
  515.         SetPort(window);    
  516.         gIRect = window->portRect;
  517.         gIRect.top = gIRect.bottom - kScrollWidth;
  518.         gIRect.left = gIRect.right - kScrollWidth;     
  519.         EraseRect(&gIRect);
  520.         SizeWindow(window,LoWord(growSize),HiWord(growSize),true);
  521.         
  522.         ClipRect(&window->portRect);
  523.         AdjustScrollbars(window, true);
  524.         InvalRect(&window->portRect);
  525.     }
  526.  
  527. }
  528.  
  529.  
  530. //----------------------------------------------------------------------
  531. //
  532. //    UpdateWindow - update dispatcher for document windows.
  533. //                 
  534. //
  535. //----------------------------------------------------------------------
  536.  
  537. void UpdateWindow(WindowRef window) 
  538. {
  539.     GrafPtr        oldPort;
  540.     
  541.     
  542.     GetPort(&oldPort);
  543.     
  544.     SetPort(window);
  545.     BeginUpdate(window);
  546.     CustomWindowEvent(kUpdateProc, window, nil);
  547.     EndUpdate(window);
  548.     
  549.     SetPort(oldPort);
  550.  
  551. }
  552.  
  553.  
  554. //----------------------------------------------------------------------
  555. //
  556. //    DoActivate - 
  557. //                 
  558. //
  559. //----------------------------------------------------------------------
  560.  
  561. void DoActivate(WindowRef window, void *refCon)
  562. {
  563.     Boolean        becomingActive;
  564.     DocHnd        doc;
  565.     
  566.     SetPort(window);
  567.         
  568.     doc = (DocHnd)GetWRefCon(window);
  569.  
  570.     if(doc != nil && GetIsAppWindow(window)) {
  571.         becomingActive = *(Boolean *)refCon;
  572.         if (becomingActive) {
  573.             DrawGrowIcon(window);
  574.             ShowControl((**doc).hScroll);
  575.             ShowControl((**doc).vScroll);
  576.         }
  577.         else {
  578.             HideControl((**doc).hScroll);
  579.             HideControl((**doc).vScroll);
  580.             DrawGrowIcon(window);
  581.         }
  582.     }
  583.     
  584. }
  585.  
  586.